<template>
  <page-header-wrapper :title="false">
    <a-card title="采购单">
      <div class="table-page-search-wrapper">
        <a-form-model
          layout="inline"
          :model="queryParam"
        >
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="所属顾问">
                <a-input v-model="queryParam['filter[username]']" placeholder="请输入所属顾问" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="关键字/词">
                <a-input v-model="queryParam['filter[tm_name]']" placeholder="请输入关键字/词" />
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="商标类别">
                  <a-select v-model="queryParam['filter[category]']" placeholder="请选择商标类别" :allowClear="true">
                    <a-select-option v-for="(item,index) in categoryOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="国家">
                  <a-select v-model="queryParam['filter[region]']" placeholder="请选择国家" :allowClear="true">
                    <a-select-option v-for="(item,index) in regionOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="是否含转让费">
                  <a-select v-model="queryParam['filter[is_tf_fee]']" placeholder="请选择是否包含转让费" :allowClear="true">
                    <a-select-option v-for="(item,index) in tfOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="进展状态">
                  <a-select v-model="queryParam['filter[status]']" placeholder="请选择进展状态" :allowClear="true">
                    <a-select-option v-for="(item,index) in statusOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          ref="table"
          bordered
          :row-key="(record) => record.id"
          :columns="purchaseColumns"
          :data="purchaseData"
        >
          <div
            slot="filterDropdown"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-input
              v-ant-ref="c => (searchInput = c)"
              :placeholder="`搜索${column.title}`"
              :value="selectedKeys[0]"
              style="width: 188px; margin-bottom: 8px; display: block;"
              @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
              @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterCategory"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in categoryOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterRegion"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in regionOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterFee"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in tfOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterStatus"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in statusOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <template slot="username" slot-scope="text, record">
            <div><a-tag>{{text}}</a-tag></div>
            <div v-for="(item, index) in groupOpts" :key="index" style="margin: 5px 0">
              <div v-if="record.group_type == index"><a-tag>{{item}}组</a-tag></div>
            </div>
            <div><a-tag>{{record.leader_name}}</a-tag></div>
          </template>
          <template slot="offer_start" slot-scope="text, record">
            {{text}} ~ {{record.offer_end}}
          </template>
          <template slot="category" slot-scope="text">
            <a-tag color="red">{{categoryOpts[text]}}</a-tag>
          </template>
          <template slot="is_tf_fee" slot-scope="text">
            <a-tag color="red">{{tfOpts[text]}}</a-tag>
          </template>
          <template slot="region" slot-scope="text">
            {{regionOpts[text]}}
          </template>
          <template slot="status" slot-scope="text,record">
            <div v-if="text == '26'">
              <a-button size="small" type="primary" ghost @click="onFeedback(record.id)">{{statusOpts[text]}}</a-button>
            </div>
            <div v-else>
              <a-button size="small" type="primary" ghost @click="onEdit(record.id)">{{statusOpts[text]}}</a-button>
            </div>
          </template>
          <template slot="action" slot-scope="text, record">
            <a-button size="small" type="primary" @click="$router.push({ name: 'OrderDetail', params: { id: record.id } })">查看</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
    <a-modal
      centered
      title="进展状态编辑"
      v-model="visible"
      @ok="onSubmit"
    >
      <a-form-model
        ref="form"
        :model="statusForm"
      >
        <a-form-model-item label="进展状态">
          <a-radio-group v-model="statusForm.status">
            <a-radio v-for="(item,index) in purchaseOpts" :key="index" :value="index">{{item}}</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item v-if="statusForm.status == 24" prop="notes" label="采购失败备注">
          <a-textarea v-model="statusForm.notes" :rows="4" placeholder="注：如不能采购请填写原因"/>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
    <a-modal
      centered
      title="采购单反馈"
      v-model="feedbackVisible"
      @ok="onSubmitFeedback"
      width="800px"
    >
      <s-table
        bordered
        ref="table1"
        size="small"
        :row-key="(record) => record.id"
        :columns="columns"
        :data="dataSource"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <div
          slot="filterDropdown"
          slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
          style="padding: 8px"
        >
          <a-input
            v-ant-ref="c => (searchInput = c)"
            :placeholder="`搜索${column.title}`"
            :value="selectedKeys[0]"
            style="width: 188px; margin-bottom: 8px; display: block;"
            @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
            @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
          />
          <a-button
            type="primary"
            icon="search"
            size="small"
            style="width: 90px; margin-right: 8px"
            @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
          >
            搜索
          </a-button>
          <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
            重置
          </a-button>
        </div>
        <div
          slot="filterSelect"
          slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
          style="padding: 8px"
        >
          <a-select
            :placeholder="`选择${column.title}`"
            style="width: 188px; margin-bottom: 8px; display: block;"
            :value="selectedKeys[0]"
            @change="(value) => setSelectedKeys(value ? [value] : [])"
            @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
          >
            <a-select-option v-for="(item, index) in categoryOpts" :value="index" :key="index">{{item}}</a-select-option>
          </a-select>
          <a-button
            type="primary"
            icon="search"
            size="small"
            style="width: 90px; margin-right: 8px"
            @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
          >
            搜索
          </a-button>
          <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
            重置
          </a-button>
        </div>
      </s-table>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
  import { STable, Ellipsis } from '@/components'
  import transactionColumns from '@/columns/transaction'
  import { getEditPurchase, getPurchaseList, postEditPurchase, getTradeList, postFeedback } from '@/api/tm.shop'

  export default {
    name: 'Purchase',
    components: { STable, Ellipsis },
    data () {
      return {
        visible: false,
        feedbackVisible: false,
        advanced: true,
        queryParam: {},
        purchaseColumns: transactionColumns.purchase,
        purchaseData: parameters => {
          return getPurchaseList(Object.assign(parameters, this.queryParam)).then(res => {
            this.payTypeOpts = res.result.PayTypeOpts
            this.tfOpts = res.result.IsTfOpts
            this.groupOpts = res.result.group_type
            this.categoryOpts = res.result.category_opts
            this.statusOpts = res.result.statusOpts
            this.regionOpts = res.result.tm_region_opts
            return res.result
          })
        },
        payTypeOpts: {},
        tfOpts: {},
        categoryOpts: {},
        groupOpts: {},
        statusOpts: {},
        regionOpts: {},
        purchaseOpts: {},
        statusForm: {},
        selectedRowKeys: [],
        feedbackId: '',
        dataSource: parameters => {
          return getTradeList(Object.assign(parameters, this.queryParam)).then(res => {
            return res.result
          })
        },
        columns: [
          {
            title: '商标名称',
            dataIndex: 'tm_name',
            scopedSlots: { filterDropdown: 'filterDropdown' }
          },
          {
            title: '商标类别',
            dataIndex: 'category',
            scopedSlots: {
              filterDropdown: 'filterSelect'
            }
          }
        ]
      }
    },
    methods: {
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      resetForm () {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      onEdit (id) {
        const app = this
        app.statusForm = {}
        getEditPurchase(id).then(res => {
          if (res.status === 0) {
            app.statusForm = res.result.purchase
            app.purchaseOpts = res.result.status_items
            app.statusForm.id = id
            app.visible = true
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      onSubmit () {
        const app = this
        const data = {}
        data['purchase[status]'] = app.statusForm.status
        data['purchase[notes]'] = app.statusForm.notes
        postEditPurchase(app.statusForm.id, data).then(res => {
          if (res.status === 0) {
            app.visible = false
            app.$refs.table.refresh()
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      onFeedback (id) {
        this.feedbackVisible = true
        this.feedbackId = id
      },
      onSelectChange (selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },
      onSubmitFeedback () {
        const app = this
        const data = {}
        for (const k in app.selectedRowKeys) {
          data['trade_ids[' + k + ']'] = app.selectedRowKeys[k]
        }
        postFeedback(app.feedbackId, data).then(res => {
          if (res.status === 0) {
            app.feedbackVisible = false
            app.feedbackId = ''
            app.selectedRowKeys = []
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      handleSearch (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
      },
      handleSelect (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
      },
      handleReset (clearFilters, dataIndex) {
        clearFilters()
        delete this.queryParam['filter[' + dataIndex + ']']
      }
    }
  }
</script>

<style scoped>

</style>
